SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE  PROCEDURE [dbo].[GetTradesByIssuer]
 @IssuerName varchar(100)
, @Fund      VARCHAR(10)
 AS
BEGIN
 
 --DECLARE @Fund      VARCHAR(10)
 --DECLARE @IssuerName VARCHAR(100)

 ----SET @StartDate = '5/1/2013'
 ----SET @EndDate   = '5/5/2013'
 --SET @Fund      = 'FSIC'
 ----(FSIC, FSIC2)
 
 --SET @IssuerName ='A.P. Plasman Inc.'
 
 --SET @Fund  ='FSIC'
 --SET @IssuerName = 'Cenveo Corp.'
 SELECT     i.InstrName, t.PortfolioId, t.TradeType, t.TradeAmount, t.TradePrice, t.TradeDate, t.TradeId

 FROM       [FSFundModel].Reporting.Trade t
            INNER JOIN FSFundModel.Reporting.Asset a
            ON         t.LotId        = a.LotId
            AND        t.ExportId     = a.ExportId
            AND        t.InstrumentId = a.InstrumentId
			AND			t.PortfolioId = a.PortfolioId
			
            INNER JOIN [FSFundModel].Reporting.Instrument i
            ON         a.ExportId     = i.ExportId
            AND        t.InstrumentId = i.InstrumentId
			INNER JOIN [FSFundModel].Reporting.Issuer iss
			ON a.ExportId = iss.ExportId
			AND i.IssuerId = iss.IssuerId


 
 
 
WHERE	
	iss.IssuerName = @IssuerName

AND     t.PortfolioId IN
                             (SELECT DISTINCT p.PortfolioId
                             
                             FROM             [FSFundModel].Reporting.Portfolio p
                             WHERE            p.FundId = @Fund)

AND t.exportid = (SELECT MAX(ExportId) FROM   [FSFundModel].Reporting.Export WHERE FundId =@Fund)
ORDeR BY t.TradeId, t.TradeDate DESC 



END

GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Dbo', 'SCHEMA', N'dbo', 'PROCEDURE', N'GetTradesByIssuer', NULL, NULL
GO
